<%--
  Created by IntelliJ IDEA.
  User: dhd
  Date: 2024/6/6
  Time: 9:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>创建储存空间</title>
    <script src="${pageContext.request.contextPath}/js/jquery-1.11.3.js"></script>
</head>
<body>
<h1>创建储存空间</h1>
<form action="#">
    <div class="form-group mb-3">
        <label for="bucketName">存储空间名称</label>
        <input type="text" class="form-control" id="bucketName" name="bucketName">
        <small class="form-text text-muted">长度限制为63个字符</small>
    </div>


    <label>存储类型</label>
    <input type="radio" name="type" id="standardStorage" value="标准存储" checked>标准存储
    <input type="radio" name="type" id="lowAccessStorage" value="低频访问存储">低频访问存储
    <input type="radio" name="type" id="archivalStorage" value="归档存储">归档存储
    <br>

    <label>读写权限</label>
    <input type="radio" name="permission" id="private" value="Pri" checked>私有（Pri）
    <input type="radio" name="permission" id="readable" value="R">公共读（R）
    <input type="radio" name="permission" id="readWrite" value="RW">公共读写（RW）
    <br>
    <input type="button" value="确定" id="queding">
    <input type="button" value="取消" id="quxiao">
</form>
</body>
</html>
<script>

    //校验名字是否重复
    function verifyName() {
        let bucketName = $('#bucketName').val();
        let flagName = false;
        $.ajax({
            url: "${pageContext.request.contextPath}/Oss.action",
            type: 'get',
            data: {opr: 'selectName', bucketName},
            dataType: 'json',
            success: function (data) {
                if (data == null) {
                    let opr = "add";
                    let bucketName = $('#bucketName').val().trim()
                    if(bucketName.length===0){
                        alert("请填写储存空间名称！")
                        return;
                    }
                    //添加
                    let type = $('input[name="type"]:checked').val()
                    let permission = $('input[name="permission"]:checked').val()
                    let params = {opr, bucketName, type, permission}
                    $.ajax({
                        url: "${pageContext.request.contextPath}/Oss.action",
                        type: 'post',
                        data: params,
                        dataType: 'json',
                        success: function (data) {
                            console.log(data);
                            if (data.code == 'ok') {
                                window.location.href = "ccSpace.jsp";
                            }
                        },
                        error: function () {
                            alert("新增失败!");
                        }
                    })
                }else {
                    alert("存储空间名称已存在！，请重新输入！")
                }

            }
        })
        return flagName;
    }


    //点击确定按钮
    $('#queding').click(function () {
        verifyName();
    })

    //点击取消按钮返回主页面
    $('#quxiao').click(function () {
        window.location.href = "ccSpace.jsp";
    })
</script>
